package com.jamlu.framework.presenter.impl;

import com.jamlu.framework.presenter.IBaseRxPresenter;
import com.jamlu.framework.ui.view.IBaseView;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Author DMing
 * Created at 2018/02/23.
 * Description
 * p层基类
 */

public class BaseRxPresenter<V extends IBaseView> implements IBaseRxPresenter {

    private V mView;
    private CompositeDisposable mCompositeDisposable; //这个类的内部是由Set<Subscription> 维护订阅者

    public BaseRxPresenter(V iView) {
        mView = iView;
    }

    /**
     * 获取视图
     *
     */
    @Override
    public V getView() {
        return mView;
    }

    /**
     * 添加订阅器到订阅池
     *
     * @param d rx disposable
     */
    @Override
    public void addDisposable(Disposable d) {
        //判断如果为空或者取消订阅就重新创建订阅池
        getDisposables();
        this.mCompositeDisposable.add(d);
    }

    /**
     * 取消订阅
     */
    @Override
    public void disposeAll() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    @Override
    public CompositeDisposable getDisposables() {
        if (this.mCompositeDisposable == null || mCompositeDisposable.isDisposed()) {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        return mCompositeDisposable;
    }

}
